package com.bzy.blp.upms.service.impl;

import com.bzy.blp.mapper.TSystemAppInfoDatMapper;
import com.bzy.blp.model.TSystemAppInfoDat;
import com.bzy.blp.model.TSystemAppInfoDatCriteria;
import com.bzy.blp.model.TSystemResourceInfoDat;
import com.bzy.blp.upms.mapper.ResourceInfoMapper;
import com.bzy.blp.upms.service.AppService;
import com.bzy.blp.upms.service.MenuService;
import com.bzy.blp.upms.service.UserService;
import com.bzy.blp.upms.vo.MainmenuVO;
import com.bzy.common.exception.LogicException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    UserService userService;
    @Autowired
    ResourceInfoMapper resourceInfoMapper;
    @Autowired
    TSystemAppInfoDatMapper tSystemAppInfoDatMapper;
    @Autowired
    AppService appService;

    @Override
    public List<MainmenuVO> queryMenu(String userName, String appKey) throws Exception {
        List<MainmenuVO>  mainmenuVos = queryMainByUserAndAppKey(userName, appKey);
        return mainmenuVos;
    }

    @Override
    public List<MainmenuVO> queryMenuNew(String userName, String appKey) throws Exception {
        List<MainmenuVO>  mainmenuVos = queryMainByUserAndAppKey(userName, appKey);
        if(mainmenuVos.get(0).getParent_id().equals(0L)){
            mainmenuVos = mainmenuVos.get(0).getNodes();
        }
        for(MainmenuVO mainmenuVo : mainmenuVos){
            mainmenuVo.setNodes(null);
        }
        return mainmenuVos;
    }


    @Override
    public List<MainmenuVO> queryMenuNewChlid(String userName, String appKey, String resource_id) throws Exception {
        List<MainmenuVO>  mainmenuVos = queryMainByUserAndAppKey(userName, appKey);
        if(mainmenuVos.get(0).getParent_id().equals(0L)){
            mainmenuVos = mainmenuVos.get(0).getNodes();
        }
        List<MainmenuVO> menuChlids = new ArrayList<MainmenuVO>();
        for(MainmenuVO mainmenuVo : mainmenuVos){
            if(mainmenuVo.getResource_id().longValue() == Long.parseLong(resource_id)){
                menuChlids = mainmenuVo.getNodes();
            }
        }
        return menuChlids;
    }


    private List<MainmenuVO> queryMainByUserAndAppKey(String userName, String appKey) throws Exception {
        Long userId = userService.findUseridByUsername(userName);
        TSystemAppInfoDat appInfo = appService.findAppIdByAppKey(appKey);
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("appId", appInfo.getAppId());
        List<TSystemResourceInfoDat> resourceInfoDats = resourceInfoMapper.queryMenu(params);
        List<MainmenuVO>  mainmenuVos = treeMainmenuVO(resourceInfoDats);
        return mainmenuVos;
    }

    private List<MainmenuVO> treeMainmenuVO(List<TSystemResourceInfoDat> dataList) throws Exception {
        if (dataList == null) return null;
        List<MainmenuVO> mainmenuVoTree = new ArrayList<MainmenuVO>();
        List<MainmenuVO> mainmenuVoList = new ArrayList<MainmenuVO>();
        for (TSystemResourceInfoDat node : dataList) {
            mainmenuVoList.add(convert(node));
        }
        for (MainmenuVO root : mainmenuVoList) {
            Boolean isRoot = true;
            for (MainmenuVO node : mainmenuVoList) {
                if (root.getParent_id().equals(node.getResource_id())) {
                    isRoot = false;
                    break;
                }
            }
            if (isRoot) {
                mainmenuVoTree.add(root);
            }
            for (MainmenuVO node : mainmenuVoList) {
                if (node.getParent_id().equals(root.getResource_id())) {
                    if (root.getNodes() == null) {
                        List<MainmenuVO> myChildrens = new ArrayList<MainmenuVO>();
                        myChildrens.add(node);
                        root.setNodes(myChildrens);
                    } else {
                        root.getNodes().add(node);
                    }
                }
            }
        }
        return mainmenuVoTree;
    }

    private MainmenuVO convert(TSystemResourceInfoDat src) {
        MainmenuVO dest = new MainmenuVO();
        dest.setResource_id(src.getResourceId());
        dest.setParent_id(src.getParentId());
        dest.setParent_path(src.getParentPath());
        dest.setPermissions_code(src.getPermissionsCode());
        dest.setResource_name(src.getResourceName());
        dest.setResource_type(src.getResourceType());
        dest.setResource_request_uri(src.getResourceRequestUri());
        dest.setResource_image_uri(src.getResourceImageUri());
        dest.setPriority(src.getPriority());
        return dest;
    }
}
